package com.laboratory.serviceprovide.workBench.service.impl;

import com.Laboratory.workBench.pojo.notice;
import com.Laboratory.workBench.service.noticeService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laboratory.serviceprovide.constant.constantArgs;
import com.laboratory.serviceprovide.workBench.mapper.noticeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/11 18:14
 * @Version 1.0
 */
@DubboService(interfaceClass = noticeService.class,version = "1.0.0")
public class noticeServiceImpl implements noticeService {
    @Resource
    private noticeMapper noticeMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<notice> getAllNotices() {
        List<notice> resultList=null;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if(ops.get(constantArgs.REDIS_NOTICES_KEY) == null){
            /*缓存为空*/
            resultList=noticeMapper.getAllNotices();
            if (resultList.size()>0){
                /*将查询到的数据保存到redis中*/
                ops.set(constantArgs.REDIS_NOTICES_KEY, JSON.toJSONString(resultList), 15 * 60, TimeUnit.SECONDS);
            }
        }else {
            /*缓存不为空*/
            resultList=JSON.parseObject(ops.get(constantArgs.REDIS_NOTICES_KEY),new TypeReference<List<notice>>(){});
        }
        return resultList;
    }
}
